package mk.edu.ii.navigatemacedonia;


import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SiteAdapter extends ArrayAdapter<Site> {
	int resource;
	View mview;
	public SiteAdapter(Context _context,
	int _resource,
	List<Site> _items) {
	super(_context, _resource, _items);
	resource = _resource;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View siteView;
		
		Site item=getItem(position);
		
		if (convertView == null) {
			siteView = new LinearLayout(getContext());
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater vi;
			vi = (LayoutInflater)getContext().getSystemService(inflater);
			vi.inflate(resource, (ViewGroup)siteView, true);
			} else {
			siteView = (LinearLayout) convertView;
			}
		TextView distanceView = (TextView)siteView.findViewById(R.id.rowDistance);
		TextView sitenameView = (TextView)siteView.findViewById(R.id.row);
		ImageView siteBearingView = (ImageView)siteView.findViewById(R.id.rowBearing);
		ImageView siteCategory = (ImageView)siteView.findViewById(R.id.item_category);
		ImageView siteRating = (ImageView)siteView.findViewById(R.id.item_rating);
		
		distanceView.setText(item.getSiteDistance());
		if(item.getSiteName().length()>17)
			sitenameView.setText(item.getSiteName().substring(0, 15)+"...");
		else
			sitenameView.setText(item.getSiteName());
		siteBearingView.setImageResource(getBearing(item.getSiteBearing()));
		siteCategory.setImageResource(getCategoryResID(item.getSiteCategory()));
		
		siteRating.setImageResource(getRating(item.getSiteRating()));
		return siteView;
	}
	
	private int getCategoryResID(String siteCategory) {
		
		ArrayAdapter<CharSequence> a=ArrayAdapter.createFromResource(getContext(),R.array.category,android.R.layout.simple_spinner_item);
		//return a.getPosition(siteCategory);
		switch (a.getPosition(siteCategory)) {
		case 0:{
				return R.drawable.white;
		}
		case 1:{
			return R.drawable.museum; 
		}
		case 2:{
			return R.drawable.shoping;
		}
		case 3:{
			return  R.drawable.attraction; 
            
		}
		case 4:{
			return R.drawable.accomodation;
		}
		case 5:{
			return R.drawable.food;
		}
		case 6:{
			return  R.drawable.culture;
		}
		case 7:{
			return R.drawable.infrastructure;
		}
		}
		return R.drawable.icon;
	}
	
	int getBearing(float angle){
		if(angle >= -22 && angle < 22)
			return R.drawable.n;
		if(angle >= 22  && angle < 67)
			return R.drawable.ne;
		if(angle >= 67  && angle < 112)
			return R.drawable.e;
		if(angle >= 112  && angle < 157)
			return R.drawable.se;
		if(angle >= -157 && angle <-112)
			return R.drawable.sw;
		if(angle >= -112 && angle < - 67)
			return R.drawable.w;
		if(angle >= -67 && angle < -22)
			return R.drawable.nw;
		else
			return R.drawable.s;
	}
	int getRating(float rating){
		if(rating >= 4.8)
			return R.drawable.five;
		if(rating >=4.3  && rating < 4.8)
			return R.drawable.four_half;
		if(rating >=3.8  && rating < 4.3)
			return R.drawable.four;
		if(rating >=3.3  && rating < 3.8)
			return R.drawable.three_half;
		if(rating >=2.8  && rating < 3.3)
			return R.drawable.three;
		if(rating >=2.3  && rating < 2.8)
			return R.drawable.two_half;
		if(rating >=1.8  && rating < 2.3)
			return R.drawable.two;
		if(rating >=1.3  && rating < 1.8)
			return R.drawable.one_half;
		else
			return R.drawable.one;
	}
}
